package com.sxc.study;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by sunxunchao on 2019/9/9
 */
public class MAdapter extends RecyclerView.Adapter<MAdapter.MViewHolder> {

    private List<String> datas = new ArrayList<>();

    public MAdapter(List<String> datas) {
        this.datas = datas;
    }

    public void addDatas(List<String> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_m_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder mViewHolder, int i) {
        mViewHolder.tv.setText(datas.get(i));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class MViewHolder extends RecyclerView.ViewHolder {

        private TextView tv;

        public MViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}
